package com.module.admin.app.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.module.admin.app.dao.AppUserDao;
import com.module.admin.app.pojo.AppUser;
import com.module.admin.app.service.AppUserService;
import com.module.admin.sys.utils.SysUserUtil;
import com.system.comm.model.Page;
import com.system.handle.model.ResponseFrame;

/**
 * app_user的Service
 * @author admin
 * @date 2020-12-27 23:17:10
 * @version V1.0.0
 */
@Component
public class AppUserServiceImpl implements AppUserService {

	@Autowired
	private AppUserDao appUserDao;
	
	/**
	 * 保存或修改
	 * @param appUser
	 * @return
	 */
	@Override
	public ResponseFrame save(AppUser appUser) {
		ResponseFrame frame = new ResponseFrame();
		AppUser org = get(appUser.getAiId(), appUser.getUserId());
		if(org == null) {
			appUserDao.save(appUser);
		}
		frame.setSucc();
		return frame;
	}
	
	/**
	 * 根据aiId获取对象
	 * @param aiId
	 * @return
	 */
	@Override
	public AppUser get(String aiId, Integer userId) {
		return handleDtl(appUserDao.get(aiId, userId));
	}

	/**
	 * 分页获取对象
	 * @param appUser
	 * @return
	 */
	@Override
	public Page<AppUser> pageQuery(AppUser appUser) {
		appUser.setDefPageSize();
		int total = appUserDao.findAppUserCount(appUser);
		List<AppUser> data = null;
		if(total > 0) {
			data = appUserDao.findAppUser(appUser);
			for (AppUser item : data) {
				handleDtl(item);
			}
		}
		Page<AppUser> page = new Page<AppUser>(appUser.getPage(), appUser.getSize(), total, data);
		return page;
	}
	/**
	 * 处理字典等详细信息的方法
	 * @param appUser
	 */
	private AppUser handleDtl(AppUser appUser) {
		if (appUser != null) {
			appUser.setCreateUserName(SysUserUtil.getNickname(appUser.getCreateUserId()));
		}
		return appUser;
	}
	
	/**
	 * 根据aiId删除对象
	 * @param aiId
	 * @return
	 */
	@Override
	public ResponseFrame delete(String aiId, Integer userId) {
		ResponseFrame frame = new ResponseFrame();
		appUserDao.delete(aiId, userId);
		frame.setSucc();
		return frame;
	}

	@Override
	public List<AppUser> findByAiId(String aiId) {
		List<AppUser> list = appUserDao.findByAiId(aiId);
		return list;
	}
}